<script>
    import {location, querystring} from 'svelte-spa-router'
    import List from '../components/list.svelte';
    import Btn from '../components/btn.svelte';
    import QueryBar from '../components/queryBar.svelte';
    import Paging from '../components/paging.svelte';
    import { onMount } from 'svelte';
    import { Post } from '../lib/net.js';
    import { SearchAgent,AgentUsersList } from '../api.js';
    import Query from '../model/query.js';
    import moment from 'moment';

    let data = {title:[ "UID","姓名","手机号","邮箱","身份证","用户余额","开单数量","加入时间","身份","代理人","状态" ],data:[]}

    let id = 0;

    let total = 0;

    var query = new Query();

    // 向列表赛数据，须先定义表头，拿到数据后做title下表映射关系.

    onMount(()=>{
        query.id = Number($querystring.replace("agent_id=",""));
        getList();
    });

    async function getList(e){

        if (e) {
            query.start = e.detail.start
            query.end = e.detail.end
            if (e.detail.types !== 1) {
                
                let ret = await Post(SearchAgent, {
                    type: e.detail.types,
                    condition: query.condition,
                });
                query.condition = "";
                query.admin = ret
                if(query.admin == -100) return
                // query.admin = '18'
            }
        }

       let ret = await Post(AgentUsersList,query)
       query.total = ret.total;
       total = ret.total;
       let _data = [];
       for(let item of ret.list) {
        let balance = 0;
        balance += Number(item.assets.balance.balance)
        balance += Number(item.assets.contract.balance)
         //let arr = [item.id,item.userinfo.name,item.phone,item.email,item.userinfo.id,balance.toFixed(2),item.orderCount,moment(item.createDate).format("YYYY-MM-DD hh:mm:ss"),"普通用户",item.inviteCode,item.state === 0 ? "正常": "冻结"];
		 let arr = [item.id,item.userinfo.name,item.phone,item.email,item.userinfo.id,balance.toFixed(2),item.orderCount,item.userinfo.show_create_date,"普通用户",item.inviteCode,item.state === 0 ? "正常": "冻结"];
        _data.push(arr);
       }
        data.data = _data;
    }

    function setPage(page){
        query.page = page;
        getList();
    }

</script>

<div class="view">
    <div class="container  backBlue colorWhite borderRadius">
        <div class="columns">
            <div class="column is-2">
                <nav class="level">
                    <div class="level-item has-text-centered">
                        <div>
                            <p class="heading">他邀请的人数</p>
                            <p class="font25 colorWhite">{total}</p>
                        </div>
                    </div>
                </nav>
            </div>
        </div>
    </div>
    <div class="container">
        <QueryBar query={query} on:message={getList}/>
    </div>
    <div class="container">
        <List data={data}/>
    </div>
    <div class="container rowReverse">
        {#if query.total !== 0}
        <Paging page={query} handler={setPage}/>
        {/if}
    </div>
</div>

<style>
    .container {
        margin: 20px;
    }
    .info {
        background: #fff;
    }
    .btn {
        line-height: 5px;
        top: 50%;
        transform: translateY(10%);
    }
</style>
